package com.youku.core.view.components
{
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.GlobalService;
   import com.youku.GlobalServiceKeys;
   import stream.event.YoukuStreamEvent;
   import stream.youku.YoukuStreamConfig;
   import com.youku.P2PConfig;
   import flash.net.NetConnection;
   import stream.youku.YoukuStream;
   import flash.events.NetStatusEvent;
   import com.youku.utils.DelayTimer;
   import com.youku.events.CoreEvent;
   import com.youku.events.PlayerControlEvent;
   import com.youku.core.view.events.BaseVideoEvent;
   import com.youku.PlayerConfig;
   import com.youku.core.model.vo.VideoSegmentData;
   
   public class YoukuStreamVideo extends BaseVideo
   {
      
      public function YoukuStreamVideo(param1:Array, param2:Array, param3:Array, param4:Array) {
         super();
         this._urlList = param1;
         this._filesizeList = param2;
         this._durationList = param3;
         this._fileidList = param4;
         super.NORMAL_BUFFER_TIME = 15;
      }
      
      protected var _urlList:Array;
      
      protected var _filesizeList:Array;
      
      protected var _durationList:Array;
      
      protected var _fileidList:Array;
      
      protected function get playerProxy() : IPlayerProxy {
         return GlobalService.getService(GlobalServiceKeys.PLAYER_PROXY) as IPlayerProxy;
      }
      
      public function resetUrlList(param1:Array) : void {
         if(_ns)
         {
            _ns.dispatchEvent(new YoukuStreamEvent(YoukuStreamEvent.YOUKUSTREAM_SET_URLLIST,false,false,param1));
         }
      }
      
      override protected function createItems() : void {
         var _loc1_:YoukuStreamConfig = null;
         _loc1_ = new YoukuStreamConfig();
         _loc1_.urlList = this._urlList;
         _loc1_.fileSizeList = this._filesizeList;
         _loc1_.durationList = this._durationList;
         _loc1_.fileidList = this._fileidList;
         _loc1_.area_code = _playListData.controller.area_code;
         _loc1_.dma_code = _playListData.controller.dma_code;
         _loc1_.videoID = _playListData.videoId;
         _loc1_.type = _playListData.fileType;
         _loc1_.lang = _playListData.currentLangID;
         _loc1_.sid = _playListData.sid;
         _loc1_.ct = _playListData.categories.toString();
         _loc1_.isP2PAllow = P2PConfig.isYKUseP2P;
         _nc = new NetConnection();
         _nc.connect(null);
         _ns = new YoukuStream(_nc);
         _nsObj = {};
         var _loc2_:Object = {};
         _loc2_.onMetaData = this.onMetaData;
         _ns.client = _loc2_;
         _ns.dispatchEvent(new YoukuStreamEvent(YoukuStreamEvent.YOUKUSTREAM_SET_CONFIG,false,false,_loc1_));
         _ns.addEventListener(YoukuStreamEvent.PLAY_ONE_STREAM_END,this.onOneStreamEnd);
         _ns.addEventListener(NetStatusEvent.NET_STATUS,this.onNetStatus);
         _ns.addEventListener(YoukuStreamEvent.DOWNLOAD_ERROR_WARNING,this.onDownloadWarning);
         _ns.addEventListener(YoukuStreamEvent.METADATA_LOAD_ERROR_WARNING,this.onMetadataLoadWarning);
         _ns.addEventListener(YoukuStreamEvent.METADATA_TYPE_ERROR_WARNING,this.onMetadataTypeWarning);
         _ns.addEventListener(YoukuStreamEvent.DOWNLOAD_ERROR_FATAL,this.onDownloadFatal);
         _ns.addEventListener(YoukuStreamEvent.METADATA_LOAD_ERROR_FATAL,this.onMetadataLoadFatal);
         _ns.addEventListener(YoukuStreamEvent.METADATA_TYPE_ERROR_FATAL,this.onMetadataTypeFatal);
         _ns.addEventListener(YoukuStreamEvent.YOUKUSTREAM_METADATA_HTTP_EARLY_REQUEST_ERROR,this.onMetadataEarlyRequestFatal);
         _ns.addEventListener(YoukuStreamEvent.YOUKUSTREAM_NO_NEED,this.onYoukuStreamNoNeed);
         _ns.addEventListener(YoukuStreamEvent.YOUKUSTREAM_GET_DEBUG_LOCALID,this.onGetDebugLocalID);
         _ns.addEventListener(YoukuStreamEvent.YOUKUSTREAM_REMOVE_PEER_URL,this.onGetP2PRemoveUrl);
         _ns.addEventListener(YoukuStreamEvent.YOUKUSTREAM_IS_USE_LIVE,this.onIsUerLive);
         _ns.bufferTime = 2;
         _delayTimer = new DelayTimer(_ns,_num);
      }
      
      private function onGetDebugLocalID(param1:YoukuStreamEvent) : void {
         this.playerProxy.dispatchEvent(new CoreEvent(CoreEvent.YK_GET_DEBUG_LOCALID,param1.data));
      }
      
      private function onGetP2PRemoveUrl(param1:YoukuStreamEvent) : void {
         this.playerProxy.dispatchEvent(new CoreEvent(CoreEvent.YK_GET_P2P_REMOVE_URL,param1.data));
      }
      
      private function onIsUerLive(param1:YoukuStreamEvent) : void {
         this.playerProxy.dispatchEvent(new PlayerControlEvent(PlayerControlEvent.ADD_LIVE_PLAYER,param1.data));
      }
      
      private function onYoukuStreamNoNeed(param1:YoukuStreamEvent) : void {
         this.onSocketError(null);
      }
      
      private function onMetadataEarlyRequestFatal(param1:YoukuStreamEvent) : void {
         close();
         var _loc2_:BaseVideoEvent = new BaseVideoEvent(BaseVideoEvent.EARLY_REQUEST_ERROR);
         dispatchEvent(_loc2_);
      }
      
      private function onMetadataTypeFatal(param1:YoukuStreamEvent) : void {
         this.onSocketError(null);
      }
      
      private function onMetadataLoadFatal(param1:YoukuStreamEvent) : void {
         this.onSocketError(null);
      }
      
      private function onDownloadFatal(param1:YoukuStreamEvent) : void {
         var _loc2_:int = param1.data.num;
         this.dispatchEvent(new BaseVideoEvent(BaseVideoEvent.PLAY_MIDDLE_ERROR,{"num":_loc2_}));
      }
      
      private function onMetadataTypeWarning(param1:YoukuStreamEvent) : void {
      }
      
      private function onMetadataLoadWarning(param1:YoukuStreamEvent) : void {
      }
      
      private function onDownloadWarning(param1:YoukuStreamEvent) : void {
      }
      
      override protected function loadError() : void {
         this.close();
         var _loc1_:BaseVideoEvent = new BaseVideoEvent(BaseVideoEvent.PLAY_LOAD_ERROR,{"num":this.num});
         dispatchEvent(_loc1_);
      }
      
      private function onSocketError(param1:YoukuStreamEvent) : void {
         close();
         var _loc2_:BaseVideoEvent = new BaseVideoEvent(BaseVideoEvent.ONE_NS_CORE_ERROR,{"num":this.num});
         dispatchEvent(_loc2_);
      }
      
      override public function get num() : Number {
         if(_ns == null || !(_ns is YoukuStream))
         {
            return 0;
         }
         return (_ns as YoukuStream).curIndex;
      }
      
      override protected function onNetStatus(param1:NetStatusEvent) : void {
         super.onNetStatus(param1);
         switch(param1.info.code)
         {
            case "NetStream.Buffer.Empty":
               if(_ns.bytesLoaded < _ns.bytesTotal)
               {
                  onBufferEmpty();
               }
               break;
         }
      }
      
      private function onOneStreamEnd(param1:YoukuStreamEvent) : void {
         var _loc2_:int = param1.data.num;
         dispatchEvent(new BaseVideoEvent(BaseVideoEvent.ONE_NS_ONE_STREAM_END,{"num":_loc2_}));
      }
      
      private function onMetaData(param1:Object) : void {
         _isGotMetadata = true;
         if(param1.allDuration)
         {
            _duration = param1.allDuration;
         }
         _vw = !param1.width?320:param1.width;
         _vh = !param1.height?240:param1.height;
         var _loc2_:BaseVideoEvent = new BaseVideoEvent(BaseVideoEvent.GOT_METADATA,{"num":this.num});
         dispatchEvent(_loc2_);
         _blocking = false;
         checkWaitingActions();
      }
      
      override public function play(param1:Boolean = false) : void {
         _playOver = false;
         _closed = false;
         _ns.play("");
      }
      
      override protected function checkNSVideo() : void {
         _sendBufferLengthTimer.stop();
         _started = false;
         _loadStartSize = -1;
         _loadStartTime = -1;
         _oldTotalSize = -1;
         _speed = 0;
         _currSpeed = 0;
         _blocking = false;
         _useJumpTime = false;
         _useJumpTimeT = 0;
      }
      
      override protected function playVideo(param1:Boolean) : void {
         _ns.play("");
      }
      
      override protected function realSeek(param1:Number, param2:Boolean, param3:Boolean = false) : void {
         _closed = false;
         _playOver = false;
         setVolume(_storeVolume);
         _ns.bufferTime = 2;
         if(_closed)
         {
            _ns.play("",param1);
         }
         else
         {
            _ns.seek(param1);
         }
         _jumpT = param1;
         _mockBufferTimer.reset();
         _mockBufferTimer.start();
      }
      
      override protected function playUnloadedPart(param1:Number) : void {
         _closed = false;
         _jumpT = param1;
         _ns.seek(param1);
         _mockBufferTimer.reset();
         _mockBufferTimer.start();
      }
      
      override protected function playLoadedPart(param1:Number) : void {
         _closed = false;
         if(PlayerConfig.isSeekToKeyframe == true)
         {
            param1 = getKeyframe(param1);
         }
         _ns.seek(param1);
         checkWaitingActions();
         setVolume(_storeVolume);
         _mockBufferTimer.reset();
         _mockBufferTimer.start();
      }
      
      override public function get loadedTime() : Number {
         if(_ns is YoukuStream)
         {
            return (_ns as YoukuStream).loadedTime;
         }
         return _ns.bytesLoaded / _ns.bytesTotal * _duration;
      }
      
      override public function get nsTime() : Number {
         return _ns.time;
      }
      
      override public function resetData(param1:VideoSegmentData) : void {
         super.resetData(param1);
      }
      
      override public function get fileid() : String {
         return this._fileidList[this.num] as String;
      }
      
      override public function get fileidTime() : Number {
         var _loc1_:Number = 0;
         var _loc2_:* = 0;
         while(_loc2_ < this.num)
         {
            _loc1_ = _loc1_ + (this._durationList[_loc2_] as Number);
            _loc2_++;
         }
         return this.nsTime - _loc1_;
      }
   }
}
